package com.rxz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rxz.dao.ZsetDao;
import com.rxz.entity.Result;
import com.rxz.entity.Zset;
import com.rxz.service.ZsetService;
import com.rxz.utils.ResultUtil;
import jakarta.annotation.Resource;
import org.springframework.data.redis.core.DefaultTypedTuple;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.*;
import java.util.concurrent.TimeUnit;

@Service
public class ZsetServiceImpl extends ServiceImpl<ZsetDao, Zset> implements ZsetService {

    @Resource
    private RedisTemplate redisTemplate;

    @Resource
    private ZsetDao zsetDao;

    public static final  String SCORE_RANK = "rxz_score_rank";

    public static final  String SCORE_RXZ = "rxz_rank2";

    @Override
    public Result addZset() {
        QueryWrapper<Zset> wrapper = new QueryWrapper<>();
        List<Zset> list = zsetDao.selectList(wrapper);
        Boolean isTrue = true;
        if(!list.isEmpty()){
            for (Zset zset: list){
                Boolean add = redisTemplate.opsForZSet().add(SCORE_RXZ, zset, zset.getScore());
                isTrue = add ? true : false;
            }
        }
        redisTemplate.expire(SCORE_RXZ,10, TimeUnit.SECONDS);
        if (isTrue) {
            return ResultUtil.success("添加Zset成功！");
        }
        return ResultUtil.error(401,"添加Zset失败！");
    }

    @Override
    public Result getAllScore() {
//        Set<ZSetOperations.TypedTuple<String>> tuples = new HashSet<>();
//        QueryWrapper<Zset> wrapper = new QueryWrapper<>();
//        List<Zset> list = zsetDao.selectList(wrapper);
//        if(!list.isEmpty()){
//            for (Zset zset: list){
////                DefaultTypedTuple<String> tuple = new DefaultTypedTuple<>(zset.getZname(), zset.getScore());
////                zsetNameToTupleMap.put(zset.getId(),tuple);
////                tuples.add(tuple);
//                redisTemplate.opsForZSet().add(SCORE_RANK,zset, zset.getScore());
//            }
//        }
//        redisTemplate.opsForZSet().add(SCORE_RANK,tuples);
//        Set<ZSetOperations.TypedTuple<String>> rangeWithScore = redisTemplate.opsForZSet().reverseRangeWithScores(SCORE_RANK,0,5);
        Set range = redisTemplate.opsForZSet().reverseRange(SCORE_RXZ, 0, -1);
        return ResultUtil.success(range);
    }

    @Override
    public Result getOne(Integer id) {
        QueryWrapper<Zset> wrapper = new QueryWrapper<>();
        wrapper.eq("id",id);
        Zset one = zsetDao.selectOne(wrapper);
        Long rank = redisTemplate.opsForZSet().reverseRank(SCORE_RXZ, one) + 1;
        System.out.println("排名信息是：" + rank);
        if(rank != null && rank >= 0){
            Map<Long,Zset> map = new HashMap<>();
            map.put(rank,one);
            return ResultUtil.success(map);
        }
        return ResultUtil.error(401,"没有该id信息！");
    }
}
